import {
  request
} from "../../request/index.js";
import regeneratorRuntime from "../../lib/runtime/runtime";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
        id: 0,
        value: "体验问题",
        isActive: true
      },
      {
        id: 1,
        value: "商品、商家投诉",
        isActive: false
      },
    ],
    chooseImgs: [],
    textVal: "",
  },
  UpLoadImgs: [],

  // 标题点击事件
  handleTabsItemChange(e) {
    const {
      index
    } = e.detail;
    let {
      tabs
    } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
    this.setData({
      tabs
    })
  },

  handleChooseImg(e) {
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (result) => {
        this.setData({
          chooseImgs: [...this.data.chooseImgs, ...result.tempFilePaths],
        })
      },
    });
  },

  handleRemoveImg(e) {
    const {
      index
    } = e.currentTarget.dataset;
    let {
      chooseImgs
    } = this.data;
    chooseImgs.splice(index, 1);
    this.setData({
      chooseImgs,
    });
  },

  handleTextInput(e) {
    this.setData({
      textVal: e.detail.value
    })
  },

  handleFormSubmit() {
    const {
      textVal,
      chooseImgs
    } = this.data;
    if (!textVal.trim()) {
      wx.showToast({
        title: '输入不合法！',
        icon: 'none',
        mask: true,
      })
      return;
    }

    if (chooseImgs.length != 0) {
      chooseImgs.forEach((v, i) => {
        wx.uploadFile({
          filePath: v,
          name: 'file',
          url: 'https://sm.ms/api/upload',
          formData: {},
          success: (result) => {
            // console.log(result);
            // let url = JSON.parse(result.data);
            // this.UpLoadImgs.push(url);
            // console.log(url);
            if (i === chooseImgs.length - 1) {
              wx.showToast({
                title: '提交成功',
                icon: 'success',
                duration: 1000
              })
              // 提交成功
              this.setData({
                textVal: "",
                chooseImgs: [],
              })
              wx.navigateBack({
                delta: 1,
              })
            }
          },
          fail: (err) => {
            // console.log(err);
          }
        });
      });
    } else {
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 1000
      })
      // 提交成功
      this.setData({
        textVal: "",
      });
      wx.navigateBack({
        delta: 1,
      });
    }
  },
})